"""
@File    ：backend_login_page.py
@Author  ：ixiaodu
@Date    ：2024/8/30
@Desc    ：
"""
import allure
from playwright.sync_api import expect
from page_object.base_page import BasePage


class LoginPage(BasePage):

    def __init__(self, page):
        super().__init__(page)
        self.url = "https://alpha-console.app.douya.wang/"
        self.page_login_locator_account_login = page.get_by_text("账号登录")
        self.page_login_locator_username = page.get_by_placeholder("请输入登录账号")
        self.page_login_locator_password = page.get_by_placeholder("请输入登录密码")
        self.page_login_locator_login_button = page.get_by_role("button", name="登录")
        self.page_login_success_locator = page.get_by_text("欢迎使用")

    def login(self, username, password):
        """
        登录
        :param username: 账号
        :param password: 密码
        :return:
        """
        with allure.step(f"跳转到登录页面: {self.url}"):
            self.navigate()
        with allure.step("等待系统加载完成"):
            self.page.wait_for_load_state('networkidle')
        with allure.step(f"点击账号登录"):
            self.page_login_locator_account_login.click()
        with allure.step(f"输入账号: {username}"):
            self.page_login_locator_username.fill(username)
        with allure.step(f"输入密码: {password}"):
            self.page_login_locator_password.fill(password)
        with allure.step("点击登录"):
            self.page_login_locator_login_button.click()
        with allure.step("断言欢迎使用可见"):
            expect(self.page_login_success_locator).to_be_visible()
